package cn.ranko.site.action;

import cn.ranko.bean.easyui.DataGrid;
import cn.ranko.bean.easyui.DataGridParam;
import cn.ranko.bean.easyui.Messager;
import cn.ranko.site.model.SysUsers;
import cn.ranko.site.service.SysUsersService;
import cn.ranko.utils.ErrorCode;
import com.alibaba.druid.util.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by ZJ08 on 2016/7/28.
 *
 * 用户管理控制器
 *
 */
@Controller
@RequestMapping("/site-users")
public class SysUsersAction {

    @Autowired
    SysUsersService sysUsersService;

    /**
     * 用户管理页面查看
     *
     * @return
     */
    @RequiresPermissions("site-users:view")
    @RequestMapping(value = "index.action",method = RequestMethod.GET)
    public String sysusers() {
        return "site/users/index";
    }

    /**
     * 获取用户列表
     *
     * @return
     */
    @RequiresPermissions("site-users:view")
    @ResponseBody
    @RequestMapping(value = "loadList.action",method = RequestMethod.POST)
    public DataGrid loadList(DataGridParam dataGridParam){
        DataGrid dataGrid = new DataGrid();
        int total = sysUsersService.getTotal();
        dataGrid.setTotal(total);
        List<SysUsers> rows = sysUsersService.getRows(dataGridParam);
        dataGrid.setRows(rows);
        return dataGrid;
    }

    /**
     * 显示添加用户的页面
     * @return
     */
    @RequiresPermissions("site-users:add")
    @RequestMapping(value = "showadd.action",method = RequestMethod.GET)
    public String showadd(){
        return "site/users/showedit";
    }

    /**
     * 用户保存操作
     * @return
     */
    @RequiresPermissions(value = {"site-users:edit","site-users:add"},logical = Logical.OR)
    @ResponseBody
    @RequestMapping(value = "save.action",method = RequestMethod.POST)
    public String save(){
        return "";
    }

    /**
     * 显示用户修改页面
     *
     * @return
     */
    @RequiresPermissions("site-users:edit")
    @RequestMapping(value = "showedit.action",method = RequestMethod.GET)
    public String showedit(Long id, ModelMap modelMap){

        SysUsers users = sysUsersService.getByUserId(id);
        modelMap.addAttribute("users",users);

        return "site/users/showedit";
    }

    /**
     * 用于设置账户的密码
     *
     * @return
     */
    @RequiresPermissions("site-users:config")
    @RequestMapping(value = "config.action",method = RequestMethod.GET)
    public String config(){
        return "site/users/config";
    }

    /**
     * 进行删除操作
     *
     * @return
     */
    @RequiresPermissions("site-users:delete")
    @RequestMapping(value = "delete.action",method = RequestMethod.GET)
    @ResponseBody
    public Messager delete(String ids){
        Messager message = new Messager();
        int affectrows = sysUsersService.deleteByIds(ids);
        if(affectrows==0){
            message.setCode(ErrorCode.UPDATE_FAIL);
        }
        return message;
    }

}
